package com.mxchip.livestar.av.screen

import android.content.Context
import android.graphics.RectF
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration

class CameraTouchControl(val root: View) {

    var context = root.context
    val dm = context.resources.displayMetrics

    // 屏幕的宽度
    private var mScreenWidth: Float = dm.widthPixels.toFloat()

    // 屏幕的高度
    private var mScreenHeight: Float = dm.heightPixels.toFloat()

    // 小视频的高度
    private val mThumbnailHeight: Float = mScreenHeight / 4

    // 小视频的宽度
    private val mThumbnailWidth: Float = mScreenWidth / 4

    //距离屏幕的最小距离
    private val mMargin: Int = dip2px(context, 2f)

    // 记录小视频的坐标
    private val mThumbnailRect: RectF = RectF(mScreenWidth - mThumbnailWidth - mMargin.toFloat(),
            mScreenHeight - mMargin, mScreenWidth - mMargin, mScreenHeight - mMargin.toFloat() - mThumbnailHeight)

    // 记录小视频的坐标
    private val mThumbnailRect2: RectF = RectF(mScreenWidth - mThumbnailWidth - mMargin.toFloat(),
            mScreenHeight - mMargin - mThumbnailHeight, mScreenWidth - mMargin, mScreenHeight - mMargin.toFloat() - mThumbnailHeight * 2)

    // 记录小视频的坐标
    private val mThumbnailRect3: RectF = RectF(mScreenWidth - mThumbnailWidth - mMargin.toFloat(),
            mScreenHeight - mMargin - 2 * mThumbnailHeight, mScreenWidth - mMargin, mScreenHeight - mMargin.toFloat() - mThumbnailHeight * 3)

    // 记录小视频的坐标
    private val mThumbnailRect4: RectF = RectF(mScreenWidth - mThumbnailWidth - mMargin.toFloat(),
            mScreenHeight - mMargin - 3 * mThumbnailHeight, mScreenWidth - mMargin, mScreenHeight - mMargin.toFloat() - mThumbnailHeight * 4)

    //最小的滑动距离
    private val mTouchSlop: Int = ViewConfiguration.get(context).scaledPagingTouchSlop

    // 标识符，判断手指按下的范围是否在小视频的坐标内
    private var mTouchThumbnail = false
    private var mTouchThumbnail2 = false
    private var mTouchThumbnail3 = false
    private var mTouchThumbnail4 = false

    // 标识符，判断手指是移动小视频而不是点击小视频
    private var isMoveThumbnail = false

    // 按下时手指的x坐标值
    private var mDownX = 0f

    // 按下时手指的y坐标值
    private var mDownY = 0f

    private var mLastYLength = 0f
    private var mLastXLength = 0f
    private var mOnGestureListener: VideoPlayerOnGestureListener? = null
    private var mGestureDetector: GestureDetector? = null
    private var mVideoGestureListener: VideoGestureListener? = null

    //横向偏移检测，让快进快退不那么敏感
    private val offsetX = 1
    private var hasFF_REW = false
    val NONE = 0
    val VOLUME = 1
    val BRIGHTNESS = 2
    val FF_REW = 3

    init {

        mOnGestureListener = VideoPlayerOnGestureListener()
        mGestureDetector = GestureDetector(context, mOnGestureListener)
        //取消长按，不然会影响滑动
//        mGestureDetector!!.setIsLongpressEnabled(false)
        root.setOnTouchListener { _, event ->
            return@setOnTouchListener mGestureDetector!!.onTouchEvent(event)
        }
    }


    fun setVideoGestureListener(videoGestureListener: VideoGestureListener) {
        mVideoGestureListener = videoGestureListener
    }


    private val TAG = "WlCameraView"

    internal var mScrollMode = NONE

    inner class VideoPlayerOnGestureListener : GestureDetector.SimpleOnGestureListener() {

        override fun onDown(event: MotionEvent): Boolean {
            Log.d(TAG, "onDown: ")
            hasFF_REW = false
            //每次按下都重置为NONE
            mScrollMode = NONE
            if (mVideoGestureListener != null) {
                mVideoGestureListener!!.onDown(event)
            }
            mDownX = event.x
            mDownY = event.y
            if (mDownX > mThumbnailRect.left && mDownX < mThumbnailRect.right
                    && mDownY > mThumbnailRect.bottom && mDownY < mThumbnailRect.top) {
                mTouchThumbnail2 = false
                mTouchThumbnail3 = false
                mTouchThumbnail4 = false
                mTouchThumbnail = false
                mLastYLength = 0f
                mLastXLength = 0f
                return true
            } else if (mDownX > mThumbnailRect2.left && mDownX < mThumbnailRect2.right
                    && mDownY > mThumbnailRect2.bottom && mDownY < mThumbnailRect2.top) {
                mTouchThumbnail = false
                mTouchThumbnail3 = false
                mTouchThumbnail4 = false
                mTouchThumbnail2 = true
                mLastYLength = 0f
                mLastXLength = 0f
                return true
            } else if (mDownX > mThumbnailRect3.left && mDownX < mThumbnailRect3.right
                    && mDownY > mThumbnailRect3.bottom && mDownY < mThumbnailRect3.top) {
                mTouchThumbnail = false
                mTouchThumbnail3 = true
                mTouchThumbnail4 = false
                mTouchThumbnail2 = false
                mLastYLength = 0f
                mLastXLength = 0f
                return true
            } else if (mDownX > mThumbnailRect4.left && mDownX < mThumbnailRect4.right
                    && mDownY > mThumbnailRect4.bottom && mDownY < mThumbnailRect4.top) {
                mTouchThumbnail = false
                mTouchThumbnail3 = false
                mTouchThumbnail4 = true
                mTouchThumbnail2 = false
                mLastYLength = 0f
                mLastXLength = 0f
                return true
            } else {
                mTouchThumbnail = false
                mTouchThumbnail2 = false
                mTouchThumbnail3 = false
                mTouchThumbnail4 = false
            }
            return true
        }


        override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
            when (mScrollMode) {
                NONE -> {
                    //offset是让快进快退不要那么敏感的值
                    if (Math.abs(distanceX) - Math.abs(distanceY) > offsetX) {
                        mScrollMode = FF_REW
                    } else {
                        if (e1.x < root.getWidth() / 2) {
                            mScrollMode = BRIGHTNESS
                        } else {
                            mScrollMode = VOLUME
                        }
                    }
                }
                VOLUME -> {
                    if (mVideoGestureListener != null) {
                        mVideoGestureListener!!.onVolumeGesture(e1, e2, distanceX, distanceY)
                    }
                    Log.d(TAG, "VOLUME: ")
                }
                BRIGHTNESS -> {
                    if (mVideoGestureListener != null) {
                        //                        mVideoGestureListener.onBrightnessGesture(e1, e2, distanceX, distanceY);
                    }
                    Log.d(TAG, "BRIGHTNESS: ")
                }
                FF_REW -> {
                    if (mVideoGestureListener != null) {
                        //                        mVideoGestureListener.onFF_REWGesture(e1, e2, distanceX, distanceY);
                    }
                    hasFF_REW = true
                    Log.d(TAG, "FF_REW: ")
                }
            }
            return true
        }


        override fun onContextClick(e: MotionEvent): Boolean {
            Log.d(TAG, "onContextClick: ")
            return true
        }

        override fun onDoubleTap(e: MotionEvent): Boolean {
            Log.d(TAG, "onDoubleTap: ")
            if (mVideoGestureListener != null) {
//                mVideoGestureListener.onDoubleTapGesture(e)
            }
            return super.onDoubleTap(e)
        }

        override fun onLongPress(e: MotionEvent) {
            Log.d(TAG, "onLongPress: ")
            super.onLongPress(e)
            mVideoGestureListener?.onLongClick()
        }

        override fun onDoubleTapEvent(e: MotionEvent): Boolean {
            Log.d(TAG, "onDoubleTapEvent: ")
            return super.onDoubleTapEvent(e)
        }

        override fun onSingleTapUp(e: MotionEvent): Boolean {
            Log.d(TAG, "onSingleTapUp: ")
            return super.onSingleTapUp(e)
        }

        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
            Log.d(TAG, "onFling: ")
            if (mVideoGestureListener != null) {
                mVideoGestureListener!!.onFling(e1, e2, velocityX, velocityY)
            }
            return super.onFling(e1, e2, velocityX, velocityY)
        }


        override fun onShowPress(e: MotionEvent) {
            Log.d(TAG, "onShowPress: ")
            super.onShowPress(e)
        }


        override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
            Log.d(TAG, "onSingleTapConfirmed: ")
            if (mTouchThumbnail) {
                mLastYLength = 0f
                mLastXLength = 0f
                //抬起手指时，如果不是移动小视频，那么就是点击小视频
                if (!isMoveThumbnail) {
                    onc?.click(1)
                    mVideoGestureListener?.onSelectSomeOne(1)
                }
                return true
            } else if (mTouchThumbnail2) {
                mLastYLength = 0f
                mLastXLength = 0f
                //抬起手指时，如果不是移动小视频，那么就是点击小视频
                if (!isMoveThumbnail) {
                    onc?.click(2)
                    mVideoGestureListener?.onSelectSomeOne(2)
                }
                return true
            } else if (mTouchThumbnail3) {
                mLastYLength = 0f
                mLastXLength = 0f
                //抬起手指时，如果不是移动小视频，那么就是点击小视频
                if (!isMoveThumbnail) {
                    onc?.click(3)
                    mVideoGestureListener?.onSelectSomeOne(3)
                }
                return true
            } else if (mTouchThumbnail4) {
                mLastYLength = 0f
                mLastXLength = 0f
                //抬起手指时，如果不是移动小视频，那么就是点击小视频
                if (!isMoveThumbnail) {
                    onc?.click(4)
                    mVideoGestureListener?.onSelectSomeOne(4)
                }
                return true
            }
            mVideoGestureListener?.onSingleTapGesture(e)
            return false
        }
    }

    /**
     * 移动小视频
     *
     * @param rectF   小视频的坐标
     * @param lengthY 在Y轴移动的距离
     * @param lengthX 在X轴移动的距离
     */
    fun moveView(rectF: RectF, lengthY: Float, lengthX: Float) {
        rectF.top = rectF.top - (lengthY - mLastYLength)
        rectF.bottom = rectF.bottom - (lengthY - mLastYLength)
        rectF.left = rectF.left + (lengthX - mLastXLength)
        rectF.right = rectF.right + (lengthX - mLastXLength)

        if (rectF.top > mScreenHeight - mMargin) {
            rectF.top = mScreenHeight - mMargin
            rectF.bottom = rectF.top - mThumbnailHeight
        }

        if (rectF.bottom < mMargin) {
            rectF.bottom = mMargin * 1f
            rectF.top = rectF.bottom + mThumbnailHeight
        }

        if (rectF.right > mScreenWidth - mMargin) {
            rectF.right = mScreenWidth - mMargin
            rectF.left = rectF.right - mThumbnailWidth
        }

        if (rectF.left < mMargin) {
            rectF.left = mMargin.toFloat()
            rectF.right = rectF.left + mThumbnailWidth
        }

        mLastYLength = lengthY
        mLastXLength = lengthX
    }

    /*fun calculateMatrix(index: Int, width: Int, height: Int): FloatArray {
        if (mScreenHeight.toInt() != height) {
            mScreenHeight = height.toFloat()
        }
        if (mScreenWidth.toInt() != width) {
            mScreenWidth = width.toFloat()
        }
        return MatrixUtil.calculateMatrix(mThumbnailRect, mScreenWidth, mScreenHeight, index)
    }*/

    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    var onc: OnClickView? = null
    fun setOnClickView(onClick: OnClickView) {
        onc = onClick
    }

    fun release() {

        mGestureDetector = null
        mOnGestureListener = null
        mVideoGestureListener = null

        root.setOnTouchListener(null)



        context = null
        onc = null


    }

    interface OnClickView {
        fun click(i: Int)
    }
}